﻿Imports System.Collections.ObjectModel

Public Class RequestsGrid
    Public Sub New(source As ObservableCollection(Of CertificateRequestItem))

        InitializeComponent()

        Dim lcw As New ListCollectionView(source)
        lcw.Filter = AddressOf Filter
        dg.ItemsSource = lcw
    End Sub

    Private Sub ButtonConfirm_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim req = DirectCast(dg.SelectedItem, CertificateRequestItem)
        CertificateRequestManager.ConfirmRequest(req)
    End Sub

    Private Sub ButtonReject_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim req = DirectCast(dg.SelectedItem, CertificateRequestItem)
        CertificateRequestManager.RejectRequest(req)
    End Sub

    Private Sub FilterChanged(sender As System.Object, e As System.Windows.RoutedEventArgs)
        DirectCast(dg.ItemsSource, ListCollectionView).Refresh()
    End Sub

    Private Function Filter(item As Object) As Boolean
        Dim ci As CertificateRequestItem = DirectCast(item, CertificateRequestItem)
        Return (ci.Status = SharedTools.CertificateRequestStatus.Accepted AndAlso cbAccepted.IsChecked.Value = True) OrElse
            (ci.Status = SharedTools.CertificateRequestStatus.Fulfilled AndAlso cbFulfilled.IsChecked.Value = True) OrElse
            (ci.Status = SharedTools.CertificateRequestStatus.Pending AndAlso cbPending.IsChecked.Value = True) OrElse
            (ci.Status = SharedTools.CertificateRequestStatus.Renewed AndAlso cbRenewed.IsChecked.Value = True) OrElse
            (ci.Status = SharedTools.CertificateRequestStatus.Refused AndAlso cbRejected.IsChecked.Value = True)
    End Function
End Class
